package com.fuyq.app;

import com.fuyq.qkl.*;
import com.fuyq.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Security;

/**
 * @Author Fuyq BG352929
 * @Date MapperControler crate at 2018/9/25 17:13
 * @Description TODO
 * @Version 1.0
 */

@Controller
@RequestMapping("/qkl")
public class MapperControler {

    private static BlockChain bc;
    private static Wallet w1;
    private static Wallet w2;
    private static Wallet coinBase;
    static {
        bc = BlockChain.getInstance();
        bc.addBlock(new Block("0"));
        //设置Bouncey作为安全提供程序
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        w1 = new Wallet();
        w2 = new Wallet();
        coinBase = new Wallet();
    }

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        return "qkl";
    }

    @ResponseBody
    @RequestMapping(value = "/mine", method = RequestMethod.GET)
    public String mine() {
        bc.addBlock(new Block(bc.getLastHash()));
        return StringUtil.getJson(bc);
    }

    @ResponseBody
    @RequestMapping(value = "/chain", method = RequestMethod.GET)
    public String chain() {
        return StringUtil.getJson(bc);
    }

    @ResponseBody
    @RequestMapping(value = "/w1", method = RequestMethod.GET)
    public String w1() {
        return "钱包1"+w1.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/w2", method = RequestMethod.GET)
    public String w2() {
        return "钱包2"+w2.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/w", method = RequestMethod.GET)
    public String w() {
        return "钱包1"+w1.toString()+"\n"+"钱包2"+w2.toString()+"\n"+"coinBase"+coinBase.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/trans", method = RequestMethod.POST)
    public String trans(String trans) {
        String[] strings = trans.split(",");
        if (strings[0].equals("1")){
            return 1+strings[2];
        }else {
            return 2+strings[2];
        }
    }

    @ResponseBody
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public String init() {
        //创建创世交易（第一笔交易），向walletA发送50个星币:
        bc.setGenesisTransaction(new Transaction(coinBase.publicKey, w1.publicKey, 50f, null));
        //手动签署《创世纪》交易。
        bc.getGenesisTransaction().generateSignature(coinBase.privateKey);
        //手动设置事务id。
        bc.getGenesisTransaction().transactionId = "0";

        //手动添加事务输出。
        bc.getGenesisTransaction().outputs.add(
                new TransactionOutput(bc.getGenesisTransaction().reciepient,
                        bc.getGenesisTransaction().value,
                        bc.getGenesisTransaction().transactionId));
        //在UTXOs列表中存储第一个事务，非常重要！
        bc.getUTXOs().put(
                bc.getGenesisTransaction().outputs.get(0).id,
                bc.getGenesisTransaction().outputs.get(0));

        System.out.println("#info:创造和开采创世纪块...... ");
        // 创世区块
        Block genesis = new Block("0");
        // 把事务放到区块里
        genesis.addTransaction(bc.getGenesisTransaction());
        // 把区块放大区块链中
        bc.addBlock(genesis);
        return "";
    }
}
